package com.lipuwater.agent.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ismartlib.utils.NetUtils;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.ui.BaseLayoutActivity;
import com.lipuwater.agent.http.ApiCallBack;
import com.lipuwater.agent.http.HttpConfig;
import com.lipuwater.agent.http.ResponseParams;
import com.lipuwater.agent.http.api.ApiRequestsBasis;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


public class FeedbackActivity extends BaseLayoutActivity implements TextWatcher {

    @Bind(R.id.edit_content)
    EditText editContent;
    @Bind(R.id.text_hint)
    TextView textHint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        initTitle(R.drawable.icon_aleft, "反馈");
        init();
    }

    private void init() {
        editContent.addTextChangedListener(this);
    }

    @OnClick(R.id.btn_post)
    public void onClick() {
        if (editContent.getText().toString().equals("")) {
            Toast("请输入反馈信息");
        } else {
            Map<String, Object> params = new HashMap<>();
            params.put("token", token);
            params.put("content", editContent.getText().toString());
            ApiRequestsBasis.PostFeedback(this, params, new ApiCallBack() {
                @Override
                public void onResponse(ResponseParams result) {
                    if (result.getStatus() == HttpConfig.success) {
                        Toast(result.getMessage());
                        finish();
                    } else if (result.getStatus() == HttpConfig.token_failure) {
                        Toast(result.getMessage());
                    } else {
                        Toast(result.getMessage());
                    }
                }

                @Override
                public void onError(Call call, Exception e) {
                    if (!NetUtils.isConnected(FeedbackActivity.this)) {
                        Toast(getString(R.string.network_fails));
                    } else {
                        Toast(getString(R.string.service_error));
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        ApiRequestsBasis.cancelRequests(this);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.length() > 0) {
            textHint.setVisibility(View.GONE);
        } else {
            textHint.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }


}
